<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of User
 *
 * @author julien
 */
require_once 'Classes/Element.php';
require_once 'Classes/Model/UserModel.php';
class User extends Element {
    
    private $username="";
    private $password="";
    private $role=0;
    private $authenticated=false;
    
    public function __construct($arrayOrId, $username="", $role=0, $password=null) {
        if(is_array($arrayOrId)){
           $this->id = $arrayOrId['ID'];
           $this->username = $arrayOrId['UserName'];
           $this->role = $arrayOrId['Role'];
           $this->password = $arrayOrId['Password'];
        }
        else {
            $this->id = $arrayOrId;
            $this->username = $username;
            $this->role = $role;
            $this->password = $password;
        }
    }
    
    public function authenticate($submitedPassword, $authType="Plain Text", $hash='sha512'){
        if($authType=="Plain Text"){
            if($this->password == hash($hash,$submitedPassword)){
                $this->authenticated = true;
                
            }
        }
        return $this->authenticated;
    }
    
    public function setUsername($username){
        $this->username = $username;
    }
    public function getUsername(){
        return $this->username;
    }
    
    public function setRole($role){
        $this->role = is_int($role) ? $role : 0;
    }
    public function getRole(){
        return $this->role;
    }
    
    public function setHashedPassword($hashedPassword){
        $this->password = $hashedPassword;
    }
    public function setCleartextPassword($password,$hash='sha512'){
        $this->password = hash($hash, $password);
    }
}

?>
